<?php

namespace XiaoZhuBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
use XiaoZhuBundle\Entity\UserBasic;
use XiaoZhuBundle\Helper\MyController;
/**
 * @Route("/acc")
 */
class AccountController extends MyController
{
    /**
     * @Route("/reg", name="AccReg")
     */
    public function AccReg()
    {
        $info = $this->getBasicInfo();
        $info["subTitle"] = "用户注册";
        return $this->render('@XiaoZhu/register.html.twig', $info);
    }

    /**
     * @Route("/reg/do", name="AccRegDo")
     */
    public function AccRegDo(Request $request)
    {
        $user = New UserBasic();
        if (!$name = $this->getParamRequired("name",$request)) {return $this->js_out("请填写用户名");}
        if ($this->chkExist("XiaoZhuBundle:UserBasic",array("name"=>$name))) {return $this->js_out("用户名已存在");}

        if (!$phone = $this->getParamRequired("phone",$request)) {return $this->js_out("请填写手机号");}
        if ($this->chkExist("XiaoZhuBundle:UserBasic",array("phone"=>$phone))) {return $this->js_out("手机号已被使用");}

        if (!$email = $this->getParamRequired("email",$request)) {return $this->js_out("请填写邮箱");}
        if ($this->chkExist("XiaoZhuBundle:UserBasic",array("email"=>$email))) {return $this->js_out("邮箱已被使用");}

        if (!$password = $this->getParamRequired("password",$request)) {return $this->js_out("请填写密码");}
        if (!$confirmPassword = $this->getParamRequired("confirmPassword",$request)) {return $this->js_out("请填写确认密码");}

        if ($password != $confirmPassword) {
            return $this->js_out("两次输入的密码不一致，请重新输入");
        }
        $user->setName($name);
        $user->setEmail($email);
        $user->setPassword(md5(md5($password)));
        $user->setPhone($phone);

        $em = $this->EM();
        $em->persist($user);
        $em->flush();
        $userSessionInfo = array(
            "username"  => $user->getName(),
            "logout"    => FALSE,
        );
        $this->setSessionUserInfo($userSessionInfo);
        return $this->js_out("注册成功",$this->generateUrl("shophome"),1);
    }

    /**
     * @Route("/login", name="AccLogin")
     */
    public function accLogin()
    {
        $info = $this->getBasicInfo();
        $info["subTitle"] = "用户登陆";
        return $this->render('@XiaoZhu/login.html.twig', $info);
    }

    /**
     * @Route("/login/do", name="AccLoginDo")
     */
    public function AccLoginDo(Request $request)
    {
        if (!$name = $this->getParamRequired("name",$request)) {return $this->js_out("请填写用户名");}
        if (!$password = $this->getParamRequired("password",$request)) {return $this->js_out("请填写密码");}
        $condition = array("name"=>$name,"password"=>md5(md5($password)));
        if (!$this->chkExist("XiaoZhuBundle:UserBasic",$condition)) {return $this->js_out("账号或密码错误");}

        $DB = $this->DB("XiaoZhuBundle:UserBasic");
        $user = $DB->findOneBy($condition);
        $userSessionInfo = array(
            "username"  => $user->getName(),
            "logout"    => FALSE,
        );
        $this->setSessionUserInfo($userSessionInfo);

        return $this->js_out("登陆成功",$this->generateUrl("shophome"),1);
    }

    /**
     * @Route("/logout", name="AccLogout")
     */
    public function AccLogout()
    {
        $this->get("session")->clear();
        $userSessionInfo = array(
            "username"  => "游客",
            "logout"    => True,
        );
        $this->setSessionUserInfo($userSessionInfo);

        return $this->js_out("成功注销，返回首页",$this->generateUrl("shophome"),1);
    }


    /**
     * @Route("/center", name="AccCenter")
     */
    public function AccCenter(){
        return $this->render('@XiaoZhu/acc_center.html.twig');
    }


    /**
     * @Route("/base", name="AccBaseInfo")
     */
    public function AccBaseInfo(){
        return $this->render('@XiaoZhu/acc_base_info.html.twig');
    }


    /**
     * @Route("/password", name="AccPassword")
     */
    public function AccPassword(){
        return $this->render('@XiaoZhu/acc_password.html.twig');
    }

}
